import { http } from "@/utils/http";

/** 部门产品配置模型定义 */
export type ProductDeptConfigModel = {
  id: number;
  productId: number;
  deptId: number;
  groupName: string;
  configKey: string;
  configValue: string;
  remark: string;
  createdAt: string;
  updatedAt: string;
};

/** 分页获取部门产品配置列表 */
export const getProductDeptConfigListApi = (data: IReqPagerData) => {
  return http.request<Resp<IRespPagerData<ProductDeptConfigModel>>>(
    "get",
    "/product-dept-config/page-list",
    { params: data }
  );
};

/** 新增部门产品配置 */
export const addProductDeptConfigApi = (data: Partial<ProductDeptConfigModel>) => {
  return http.request<Resp<null>>("post", "/product-dept-config", { data });
};

/** 更新部门产品配置 */
export const updateProductDeptConfigApi = (id: number, data: Partial<ProductDeptConfigModel>) => {
  return http.request<Resp<null>>("put", `/product-dept-config/${id}`, { data });
};

/** 删除部门产品配置 */
export const deleteProductDeptConfigApi = (id: number) => {
  return http.request<Resp<null>>("delete", `/product-dept-config/${id}`);
};

/** 根据ID获取部门产品配置详情 */
export const getProductDeptConfigByIdApi = (id: number) => {
  return http.request<Resp<{
    info: ProductDeptConfigModel
  }>>('get', '/product-dept-config/getOne', {
    params: { id }
  });
};

/** 获取部门产品配置选项列表 */
export const getProductDeptConfigOptionsApi = (params?: { productId?: number, deptId?: number, groupName?: string }) => {
  return http.request<Resp<{
    options: Array<{
      label: string;
      val: string | number;
    }>
  }>>('get', '/product-dept-config/getOptions', { params });
};

/** 获取部门产品配置组 */
export const getProductDeptGroupConfigsApi = (params: {
  productId: number;
  deptId: number;
  groupName: string;
}) => {
  return http.request<Resp<{
    configs: Array<{
      configKey: string;
      configValue: string;
      configDesc: string;
    }>;
    updatedAt: string;
  }>>(
    'get',
    '/product-dept-config/getGroupConfigs',
    { params }
  );
};

/** 保存部门产品配置组 */
export const setProductDeptGroupConfigsApi = (data: {
  productId: number;
  deptId: number;
  groupName: string;
  configs: Array<{
    configKey: string;
    configValue: string;
    configDesc?: string;
  }>
}) => {
  return http.request<Resp<null>>(
    'post',
    '/product-dept-config/setGroupConfigs',
    { data }
  );
};
